package com.b1502.store2.activity;

import android.os.Bundle;


import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.b1502.store2.R;


import com.b1502.store2.adapter.ProductAdapter;

import com.b1502.store2.model.bean.ProductBean;
import com.b1502.store2.network.PostParams;
import com.b1502.store2.network.StoreParams;

import com.b1502.store2.util.ImageLoaderUtils;
import com.b1502.store2.util.UrlUtil;


import com.b1502.store2.widget.MyListView;
import com.google.gson.Gson;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * 产品详情页面
 */
public class ProductDetailActivity extends BaseActivity {

    private ImageView product_image;

    private ImageView product_image2;
    private TextView product_name,product_leibie,product_peice,product_guige,product_yixuan,
            product_manzeng,product_manjian;
    private MyListView listView;
    private TextView product_name2,product_leibie2,product_peice2,product_guige2,product_yixuan2,
            product_manzeng2,product_manjian2;
    private MyListView listView2;
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        //getProduct("productId", "e4b77948-00b3-48e5-b8ab-6cc943d0f276");
        //addItem(1,"e4b77948-00b3-48e5-b8ab-6cc943d0f276");
        options= ImageLoaderUtils.initOptions();
        String productId = getIntent().getStringExtra("productId");
        initView();
        getProduct("productId",productId);

    }

    public void initView(){


        product_image=  (ImageView) findViewById(R.id.product_image);
        product_name = (TextView) findViewById(R.id.product_name);
        product_leibie= (TextView) findViewById(R.id.product_leibie);
        product_peice= (TextView) findViewById(R.id.product_peice);
        product_guige= (TextView) findViewById(R.id.product_guige);
        product_yixuan= (TextView) findViewById(R.id.product_yixuan);
        product_manzeng= (TextView) findViewById(R.id.product_manzeng);
        product_manjian= (TextView) findViewById(R.id.product_manjian);
        listView = (MyListView) findViewById(R.id.product_list);

        product_image2=  (ImageView) findViewById(R.id.product_image2);
        product_name2 = (TextView) findViewById(R.id.product_name2);
        product_leibie2= (TextView) findViewById(R.id.product_leibie2);
        product_peice2= (TextView) findViewById(R.id.product_peice2);
        product_guige2= (TextView) findViewById(R.id.product_guige2);
        product_yixuan2= (TextView) findViewById(R.id.product_yixuan2);
        product_manzeng2= (TextView) findViewById(R.id.product_manzeng2);
        product_manjian2= (TextView) findViewById(R.id.product_manjian2);
        listView2 = (MyListView) findViewById(R.id.product_list2);

    }

    private void getProduct(String param, String productId) {
        StoreParams params = new StoreParams(UrlUtil.GetProduct, param, productId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {

                ProductBean productBean = new Gson().fromJson(result, ProductBean.class);
                ImageLoader.getInstance().displayImage(UrlUtil.getImageUrl(productBean.getImageUrl()),product_image,options);
                ImageLoader.getInstance().displayImage(UrlUtil.getImageUrl(productBean.getImageUrl()),product_image2,options);
                product_name.setText(productBean.getName());
                product_name2.setText(productBean.getName());
                product_leibie.setText(productBean.getProductCategoryName());
                product_leibie2.setText(productBean.getProductCategoryName());
                float v= (float) productBean.getPrice();
                product_peice.setText("价格：￥"+v);
                product_name.setText(productBean.getName());
                product_peice2.setText("价格：￥"+v);
                product_name2.setText(productBean.getName());
                if(!TextUtils.isEmpty(productBean.getUnit())){
                    product_guige.setVisibility(View.VISIBLE);
                    product_guige.setText(productBean.getUnit());
                    product_guige2.setVisibility(View.VISIBLE);
                    product_guige2.setText(productBean.getUnit());
                }else {
                    product_guige.setVisibility(View.GONE);
                    product_guige2.setVisibility(View.GONE);
                }
                List<ProductBean.PromotionsBeanX> promotions = productBean.getPromotions();
                ProductBean.PromotionsBeanX promotionsBeanX = promotions.get(0);
                List<ProductBean.PromotionsBeanX.ContentsBean> contents = promotionsBeanX.getContents();
                String description = contents.get(0).getDescription();
                if(!TextUtils.isEmpty(description)){
                    product_manzeng.setVisibility(View.VISIBLE);
                    product_manzeng.setText(description);
                    product_manzeng2.setVisibility(View.VISIBLE);
                    product_manzeng2.setText(description);
                }else {
                    product_manzeng.setVisibility(View.GONE);
                    product_manzeng2.setVisibility(View.GONE);
                }

                ProductBean.PromotionsBeanX promotionsBeanX1 = promotions.get(1);

                List<ProductBean.PromotionsBeanX.ContentsBean> contents1 = promotionsBeanX1.getContents();

                String description1 = contents1.get(0).getDescription();

                if(!TextUtils.isEmpty(description1)){
                    product_manjian.setVisibility(View.VISIBLE);
                    product_manjian.setText(description1);
                    product_manjian2.setVisibility(View.VISIBLE);
                    product_manjian2.setText(description1);
                }else {

                    product_manjian.setVisibility(View.GONE);
                    product_manjian2.setVisibility(View.GONE);
                }

                if(productBean.getArguments().size()>0){


                    List<ProductBean.ArgumentsBean> arguments = productBean.getArguments();

                    ProductAdapter productAdapter=new ProductAdapter(arguments,ProductDetailActivity.this);

                    listView.setAdapter(productAdapter);
                    listView2.setAdapter(productAdapter);

                }else {



                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    /**
     * 添加到购物车
     * @param count 购买商品数量
     * @param productId 购买商品ID
     */
    private void addItem(int count, String productId) {
        StoreParams params = new StoreParams(UrlUtil.AddItem);
        params.setAsJsonContent(true);
        PostParams postParams = new PostParams(count, productId);
        params.setBodyContent(new Gson().toJson(postParams));

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                //Log.d(TAG, result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
